﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace TaDMS.Models
{
    public class AccountRepository : Repository<Account>
    {
        public AccountRepository(DbContext model) : base(model)
        {
        }

        public Account CreateAccount(string username, string password, int roleId, int? employeeId = null)
        {
            try
            {
                var newAccount = new Account
                    {
                        EmployeeId = employeeId,
                        RoleId = roleId,
                        Username = username,
                        Password = password
                    };

                var result = DbSet.Add(newAccount);

                var row = Model.SaveChanges();

                return row > 0 ? result : null;
            }
            catch (Exception exception)
            {
                ErrorMessage = exception.Message;

                return null;
            }
        }

        public bool? ValidateUser(string username, string password)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(username) && String.IsNullOrWhiteSpace(password))
                {
                    ErrorMessage = "Username or Password is null";

                    return null;
                }

                var result = base.Contains(v => v.Username.Equals(username.Trim()) && v.Password.Equals(password.Trim()));

                return result;
            }
            catch (Exception exception)
            {
                ErrorMessage = exception.Message;

                return false;
            }
        }
    }
}